.POSIX:

#
# DO NOT EDIT makefile. YOUR CHANGES MAY GET OVERWRITTEN.
# Edit makefile.in instead and then re-generate the makefile
# with python3 -mmacrop makefile.in makefile.
#


#
# Special targets
#

all:
	:

.PHONY: check clean dependcheck dist distcheck distclean deb debupload docs \
        install install-comp install-man install-strip install-module \
	installdirs lint maintainer-clean mostlyclean \
	virtinstall pre-install post-uninstall quickcheck quicklint \
	uninstall uninstall-module uninstallfiles userinstall

.SUFFIXES:


#
# Macros
#

BANDIT = bandit
BANDITARGS = -qc pyproject.toml
CTAGS = ctags
DESTDIR =
DPKG_DEB = dpkg-deb
FLAKE8 = flake8
GPG = gpg -ab
GPGARGS = -q --batch --yes
SHELL = /bin/sh
INSTALL = install
LINTIAN = lintian
LINTIANARGS = --cfg deb/lintian.cfg --fail-on error,pedantic,warning
MYPY = mypy
PATHCHK = pathchk
PYLINT = pylint
PYTHON = python3
PIP = pip
PIPARGS = --root=$(DESTDIR) --prefix=$(prefix)
PREFIX = /usr/local
RUFF = ruff
SPHINX = sphinx-build
VENV = $(PYTHON) -m venv
VERMIN = vermin
VULTURE = vulture
VPATH = .
SAFETY = safety
SAFETYARGS = --disable-optional-telemetry --policy-file=$(safetypolicy)
TAR = pax -ws '/^/$(distname)\//' -x ustar
UNTAR = tar -x
UNZIP = gunzip -c 
ZIP = gzip

bashcompdir = $(datarootdir)/bash-completion/completions
bindir = $(exec_prefix)/bin
checks = discover
copyright = COPYRIGHT
datarootdir = $(prefix)/share
debdir = deb/$(package)_$(version)-0_all
deburl = $(forgejo_url)/debian/pool/bullseye/main/upload
deb = $(debdir).deb
distar = $(distname).tgz
distfiles = \
	INSTALL.rst \
	LICENCE.rst \
	NEWS.rst \
	README.rst \
	comp/sievemgr.bash \
	comp/sievemgr.zsh \
	deb/control.in \
	deb/copyright.in \
	docs/Makefile \
	docs/_static/html/custom.css \
	docs/changelog.rst \
	docs/command.rst \
	docs/conf.py \
	docs/config.rst \
	docs/contrib.rst \
	docs/feedback.rst \
	docs/index.rst \
	docs/install.rst \
	docs/licence.rst \
	docs/module.rst \
	docs/security.rst \
	docs/sphinx.txt \
	examples/sievepatch \
	linters.txt \
	makefile \
	makefile.in \
	man/sieve.cf.5 \
	man/sievemgr.1 \
	pyproject.toml \
	setup.cfg \
	sievemgr.py \
	tests/__init__.py \
	tests/test_acap.py \
	tests/test_backup.py \
	tests/test_log.py \
	tests/test_sasl.py \
	tests/test_sieve.py \
	tests/test_term.py

distname = $(module)-$(version)
exec_prefix = $(prefix)
forgejo_url = https://codeberg.org/api/packages/$(forgejo_user)
forgejo_user = odkr
installdirs = \
	$(DESTDIR)$(man1dir) \
	$(DESTDIR)$(man5dir) \
	$(DESTDIR)$(bashcompdir) \
	$(DESTDIR)$(zshcompdir)

instlog = install.log
mandir = $(datarootdir)/man
man1dir = $(mandir)/man1
man5dir = $(mandir)/man5
man1ext = .1
man5ext = .5
module = $(package)
package = sievemgr
prefix = $(PREFIX)
safetypolicy = safety.yml
sources = \
	sievemgr.py \
	tests/__init__.py \
	tests/test_acap.py \
	tests/test_backup.py \
	tests/test_log.py \
	tests/test_sasl.py \
	tests/test_sieve.py \
	tests/test_term.py

venvdir = /opt/odkr/$(module)
version = ${__version__}
zshcompdir = $(datarootdir)/zsh-completions


#
# Build system
#

.SUFFIXES: .in

makefile: sievemgr.py makefile.in

.in:
	$(PYTHON) -mmacrop $@.in $@


#
# Installation
#

install: pre-install install-module install-man install-comp

install-man: \
	installdirs \
	$(DESTDIR)$(man1dir)/sievemgr$(man1ext) \
	$(DESTDIR)$(man5dir)/sieve.cf$(man5ext)

uninstall: uninstall-module uninstallfiles uninstalldirs post-uninstall

install-comp: \
	installdirs \
	$(DESTDIR)$(bashcompdir)/sievemgr.bash \
	$(DESTDIR)$(zshcompdir)/_sievemgr

install-strip: install

virtinstall: $(venvdir)/bin/pip

install-module:
	$(NORMAL_INSTALL)
	$(PIP) install $(PIPARGS) .
	printf 'pip: %s\n' '$(PIP)' >>$(instlog)

pre-install:
	$(PRE_INSTALL)
	set -C && : >$(instlog)

installdirs: $(installdirs)

userinstall:
	$(MAKE) -e PIPARGS=--user prefix="$$$$HOME/.local" install

virtinstall:
	@echo 'Installing SieveManager to $(venvdir) ...'
	$(MAKE) -e PIP=$(venvdir)/bin/pip PIPARGS= PREFIX='$(venvdir)' install

uninstall-module:
	$(NORMAL_UNINSTALL)
	-"$$$$(sed -n 's/pip: //p' $(instlog))" uninstall -y $(module)

uninstalldirs:
	$(POST_UNINSTALL)
	- sed -n 's/^dir: //p' $(instlog) | xargs -E '' rmdir -pv

uninstallfiles:
	$(POST_UNINSTALL)
	sed -n 's/^file: //p' $(instlog) | xargs -E '' rm -f

post-uninstall:
	$(POST_UNINSTALL)
	rm $(instlog)

$(DESTDIR)$(man1dir)/sievemgr$(man1ext): man/sievemgr.1
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r man/sievemgr.1 '$@'
	printf 'file: %s\n' '$@' >>$(instlog)

$(DESTDIR)$(man5dir)/sieve.cf$(man5ext): man/sieve.cf.5
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r man/sieve.cf.5 '$@'
	printf 'file: %s\n' '$@' >>$(instlog)

$(DESTDIR)$(bashcompdir)/sievemgr.bash: comp/sievemgr.bash
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.bash '$@'
	printf 'file: %s\n' '$@' >>$(instlog)

$(DESTDIR)$(zshcompdir)/_sievemgr: comp/sievemgr.zsh
	$(POST_INSTALL)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.zsh '$@'
	printf 'file: %s\n' '$@' >>$(instlog)

$(installdirs):
	$(PRE_INSTALL)
	mkdir -pm0755 $@
	printf 'dir: %s\n' >>$(instlog) $@

$(venvdir)/bin/pip:
	@echo 'Creating a Python virtual environment in $(venvdir) ...'
	$(PYTHON) -mvenv $(venvdir)


#
# Documentation
#

docs: man/sievemgr.1 man/sieve.cf.5

man/sievemgr.1: docs/command.rst

man/sieve.cf.5: docs/config.rst

man/sievemgr.1.gz: man/sievemgr.1

man/sieve.cf.5.gz: man/sieve.cf.5

man/sievemgr.1 man/sieve.cf.5:
	$(SPHINX) $(SPHINXARGS) -bman docs man


#
# Distribution
#

dist: dependcheck $(distar) $(distar).asc

distcheck: dist

$(distar): lint check $(distfiles)

$(distar).asc: $(distar)

dependcheck:
	$(SAFETY) scan $(SAFETYARGS) || [ $$$$? -eq 127 ]

distcheck:
	$(UNZIP) $(distar) | $(UNTAR)
	cd $(distname) && $(MAKE) -e check
	rm -rf $(distname)

$(distar):
	$(PATHCHK) $(PATHCHKARGS) -Pp $(distfiles)
	$(TAR) $(TARARGS) $(distfiles) | $(ZIP) $(ZIPARGS) >$(distar)

$(distar).asc:
	$(GPG) $(GPGARGS) $(distar)


#
# Debian package
#

deb: $(deb)

debupload: deb

$(deb): lint check \
	$(debdir)/usr/lib/python3/dist-packages/sievemgr.py \
	$(debdir)/usr/share/man/man1/sievemgr.1.gz \
	$(debdir)/usr/share/man/man5/sieve.cf.5.gz \
	$(debdir)/usr/share/bash-completion/completions/sievemgr.bash \
	$(debdir)/usr/share/zsh-completions/_sievemgr \
	$(debdir)/DEBIAN/control \
	$(debdir)/usr/share/doc/$(package)/copyright \
	$(debdir)/usr/share/doc/$(package)/changelog.gz

$(debdir)/usr/share/man/man1/sievemgr.1.gz: man/sievemgr.1

$(debdir)/usr/share/man/man5/sieve.cf.5.gz: man/sieve.cf.5

$(debdir)/DEBIAN/control: deb/control

$(debdir)/usr/share/doc/$(package)/copyright: deb/copyright

$(debdir)/usr/share/doc/$(package)/changelog.gz: NEWS.rst

deb/control: sievemgr.py deb/control.in

deb/copyright: sievemgr.py deb/copyright.in

$(deb):
	$(DPKG_DEB) $(DPKG_DEBARGS) --root-owner-group --build $(debdir)
	$(LINTIAN) $(LINTIANARGS) $@ || [ $$$$? -eq 127 ]
	GPG_TTY=$$$$(tty) debsigs --sign=maint $@

$(debdir)/usr/lib/python3/dist-packages/sievemgr.py:
	/usr/bin/pip3 install --root=$(debdir) --prefix=/usr --no-deps .
	pydir="$$$$(ls -d $(debdir)/usr/lib/python*)" \
	&& debpydir="$$$${pydir%.*}" \
	&& mv "$$$$pydir" "$$$$debpydir" \
	&& mv "$$$$debpydir/site-packages" "$$$$debpydir/dist-packages"
	find $(debdir) -type d -name __pycache__ -exec rm -rf '{}' ';' -prune

$(debdir)/usr/share/man/man1/sievemgr.1.gz:
	mkdir -pm0755 $$$$(dirname $@)
	gzip -9n <man/sievemgr.1 >$@

$(debdir)/usr/share/man/man5/sieve.cf.5.gz:
	mkdir -pm0755 $$$$(dirname $@)
	gzip -9n <man/sieve.cf.5 >$@

$(debdir)/usr/share/bash-completion/completions/sievemgr.bash:
	mkdir -pm0755 $$$$(dirname $@)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.bash $@

$(debdir)/usr/share/zsh-completions/_sievemgr:
	mkdir -pm0755 $$$$(dirname $@)
	$(INSTALL) -m u=rw,go=r comp/sievemgr.zsh $@

$(debdir)/DEBIAN/control:
	mkdir -pm0755 $$$$(dirname $@)
	$(INSTALL) -m u=rw,go=r  deb/control $@

$(debdir)/usr/share/doc/$(package)/copyright:
	mkdir -pm0755 $$$$(dirname $@)
	$(INSTALL) -m u=rw,go=r deb/copyright $@

$(debdir)/usr/share/doc/$(package)/changelog.gz:
	mkdir -pm0755 $$$$(dirname $@)
	gzip -9n <NEWS.rst >$@

debupload:
	GPG_TTY=$$$$(tty) debsigs --sign=origin $@
	curl -n --netrc-file .netrc -T$(deb) $(deburl)


#
# Cleanup
#

clean: mostlyclean

distclean: clean

maintainer-clean: distclean

clean:
	rm -rf $(package).egg-info .*_cache build deb/$(package)_* 

distclean:
	rm -f $(instlog) tags

maintainer-clean:
	@echo '$@ is for maintainers only!'
	@echo 'It deletes files that cannot be built with standard tools.'
	rm -f deb/control deb/copyright makefile man/sievemgr.1 man/sieve.cf.5

mostlyclean:
	find . -type d -name __pycache__ | xargs -E '' rm -rf
	find . -type f -name '*.bak' -exec rm '{}' +
	rm -f $(package)-*.tgz $(package)-*.tgz.asc deb/$(package)_*.deb

#
# Tags
#

tags: $(sources)
	$(CTAGS) $(CTAGSARGS) $(sources)


#
# Tests
#

check:
	$(PYTHON) -m unittest $(checks)

quickcheck:
	QUICKTEST=y $(PYTHON) -m unittest $(checks)


#
# Linters
#

lint: quicklint

lint:
	$(RUFF) check $(RUFFARGS) $(sources) || [ $$$$? -eq 127 ]
	$(FLAKE8) $(FLAKE8ARGS) $(sources) || [ $$$$? -eq 127 ]
	$(PYLINT) $(PYLINTARGS) $(sources) || [ $$$$? -eq 127 ]
	$(VULTURE) $(VULTUREFLAGS) $(sources) || [ $$$$? -eq 127 ]

quicklint:
	$(MYPY) $(MYPYARGS) $(sources) || [ $$$$? -eq 127 ]
	$(BANDIT) $(BANDITARGS) $(sources) || [ $$$$? -eq 127 ]
	$(VERMIN) $(VERMINARGS) $(sources) || [ $$$$? -eq 127 ]

